package com.example.com.common.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;


import com.example.com.common.R;
import com.example.com.common.utils.ScreenUtils;

import java.util.List;

/***
 * IDialog 基本实现
 */
public class DialogImpl extends IDialog {
    @Override
    public Dialog showMessageDialog(Context context, String title, CharSequence msg, final DialogCallBack dialogCallBack) {
        final MessageDialog dialog = new MessageDialog(context, msg, title);
        dialog.setCallBack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogCallBack != null) {
                    dialogCallBack.onClick(IDialog.YES);
                }
            }
        });
        dialog.show();
        return dialog;
    }

    @Override
    public Dialog showChooseDialog(Context context, String title, String msg, final DialogCallBack dialogCallBack) {
        return showChooseDialog(context, title, msg, null, null, dialogCallBack);
    }

    @Override
    public Dialog showChooseDialog(Context context, String title, CharSequence msg, String cancelText, String okText, final DialogCallBack dialogCallBack) {
        ChooseDialog dialog = new ChooseDialog(context, msg, title);
        dialog.setLeftButton(cancelText, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogCallBack != null)
                    dialogCallBack.onClick(IDialog.CANCEL);
            }
        });
        dialog.setRightButton(okText, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogCallBack != null)
                    dialogCallBack.onClick(IDialog.YES);
            }
        });
        dialog.show();
        return dialog;
    }


    public Dialog showItemDialog(Context context, String title,
                                 List<String> items, final DialogCallBack dialogCallBack) {
        int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        return showItemDialog(context, title, items, gravity, gravity, dialogCallBack);
    }

    public Dialog showItemDialog(Context context, String title,
                                 List<String> items, int itemGravity, int titleGravity, final DialogCallBack dialogCallBack) {
        final MenuDialog dialog = new MenuDialog(context, items, title, itemGravity, titleGravity);
        dialog.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                dialog.dismiss();
                if (dialogCallBack != null) {
                    dialogCallBack.onClick(position);
                }
            }
        });
        dialog.show();
        return dialog;

    }

    public Dialog showProgressDialog(Context context, String msg) {
        Dialog progressDialog = new Dialog(context, R.style.translucentDialog);
        progressDialog.setContentView(R.layout.dialog_progress);
        ((TextView) progressDialog.findViewById(R.id.tv_progress)).setText(msg);
        DisplayMetrics displayMetrics = ScreenUtils.getDisplayMetrics();
        int minWidth = (int) (displayMetrics.widthPixels / 2.5);
        int minHeight = (int) (displayMetrics.heightPixels / 2.5);
        int minSize = minWidth < minHeight ? minWidth : minHeight;
        View ll_container = progressDialog.findViewById(R.id.ll_container);
        ll_container.setMinimumHeight(minSize);
        ll_container.setMinimumWidth(minSize);
        progressDialog.setCancelable(true);

        progressDialog.show();
        return progressDialog;
    }

    @Override
    public Dialog shownNumberProgressDialog(Context context, String msg, int maxProgress) {
        NumbProgressDialog dialog = new NumbProgressDialog(context, msg, maxProgress);
        dialog.setCurrentProgress(0);
        dialog.show();
        return dialog;
    }


    @Override
    public Dialog showActionSheetDialog(Context context, List<String> items, DialogCallBack callback) {
        ActionSheetDialog dialog = new ActionSheetDialog(context, items, callback);
        dialog.show();
        return dialog;
    }

    @Override
    public Dialog showInputDialog(Context context, String title, CharSequence hintText, String cancelText, String okText, boolean isSingleLine, boolean isCancel, final InputDialogCallBack dialogCallBack) {
        final InputTextDialog dialog = new InputTextDialog(context, hintText, title, isSingleLine, isCancel);
        dialog.setCanceledOnTouchOutside(isCancel);
        dialog.setLeftButton(cancelText, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogCallBack != null) {
                    final String inputText = dialog.getInputText();
                    dialogCallBack.onClick(IDialog.CANCEL, inputText);
                }
            }
        });
        dialog.setRightButton(okText, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogCallBack != null) {
                    final String inputText = dialog.getInputText();
                    dialogCallBack.onClick(IDialog.YES, inputText);
                }
            }
        });
        dialog.show();
        return dialog;
    }
}
